Imports System.Transactions

'UPDATE THIS COMMENT!
''' -----------------------------------------------------------------------------
''' Project	 : BizBlox
''' Class	 : BizBlox.DOTransaction
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' Provide simple transaction capabilities to DataObjects.
''' </summary>
''' <remarks>
''' </remarks>
''' -----------------------------------------------------------------------------
Public Class DOTransaction
    Implements IDisposable

    Private m_transaction As IDbTransaction
    Private m_strConnection As String
    Private m_daObject As DataAccess

    <FlagsAttribute()> Public Enum IsolationLevel As Integer
        Chaos = 16
        ReadCommitted = 4096
        ReadUncommitted = 256
        RepeatableRead = 65535
        Serializable = 1048576
        Unspecified = -1
    End Enum

    ''' <summary>
    ''' Create an empty transaction
    ''' </summary>
    ''' <param name="isolation">Use the DOTransaction Isolation Level which maps to a real Data.IsolationLevel enum</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal isolation As IsolationLevel)
        Me.m_strConnection = DSNManager.GetConnectionString()
        Me.m_transaction = Me.BeginTransaction(isolation)
        Me.m_daObject = New DataAccess()
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Create an empty transaction.
    ''' </summary>
    ''' <remarks>
    ''' Legacy code for handling old transactions
    ''' </remarks>
    ''' <history>
    ''' 	[cinj]	26/08/2004	Created
    '''     [thoc]  09/03/2007  Updated
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub New()
        Me.m_strConnection = DSNManager.GetConnectionString()
        Me.m_transaction = Me.BeginTransaction(IsolationLevel.ReadCommitted)
        Me.m_daObject = New DataAccess()
    End Sub

    Private Function BeginTransaction() As IDbTransaction
        Return BeginTransaction(CInt(IsolationLevel.ReadCommitted))
    End Function

    Private Function BeginTransaction(ByVal isolation As Integer) As IDbTransaction
        Dim myConnection As IDbConnection = ProviderFactory.GetConnection(m_strConnection)
        Return myConnection.BeginTransaction(isolation)
    End Function

    Friend Sub AddSQL(ByVal sqlStr As String)
        Me.m_daObject.AddSQLToTransaction(sqlStr, Me.m_transaction, Me.m_transaction.Connection)
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Commit the transaction
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[cinj]	26/08/2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub Commit()
        Using scope As New TransactionScope()
            Try
                Me.m_transaction.Commit()
            Catch ex As Exception
                Try
                    Me.m_transaction.Rollback()
                    Throw New DOTransactionException("Unable to Commit Transaction.", ex)
                Catch ex2 As Exception
                    Throw New DOTransactionRollbackException("Unable to Rollback Transaction after " & ex2.ToString(), ex)
                End Try
            End Try
        End Using
    End Sub


    ''' <summary>
    ''' Explicit Transactional Rollback
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Rollback()
        Using scope As New TransactionScope()
            Try
                Me.m_transaction.Rollback()
            Catch ex As Exception
                Throw New DOTransactionRollbackException("Explicit Rollback Failed.", ex)
            End Try
        End Using
    End Sub

    'DISPOSABLE CODE
    Private disposedValue As Boolean = False        ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            Me.m_transaction.Dispose()
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class
